package cn.iocoder.yudao.module.handguard.controller.app.carrecord;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.handguard.controller.app.carrecord.vo.*;
import cn.iocoder.yudao.module.handguard.dal.dataobject.carrecord.CarRecordDO;
import cn.iocoder.yudao.module.handguard.convert.carrecord.CarRecordConvert;
import cn.iocoder.yudao.module.handguard.service.carrecord.CarRecordService;

@Tag(name = "小程序 - 用户-车子-资产记录")
@RestController
@RequestMapping("/hutai/car-record")
@Validated
public class CarRecordController {

    @Resource
    private CarRecordService carRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建用户-车子-资产记录")
    @PreAuthorize("@ss.hasPermission('hutai:car-record:create')")
    public CommonResult<Long> createCarRecord(@Valid @RequestBody CarRecordCreateReqVO createReqVO) {
        return success(carRecordService.createCarRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户-车子-资产记录")
    @PreAuthorize("@ss.hasPermission('hutai:car-record:update')")
    public CommonResult<Boolean> updateCarRecord(@Valid @RequestBody CarRecordUpdateReqVO updateReqVO) {
        carRecordService.updateCarRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户-车子-资产记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hutai:car-record:delete')")
    public CommonResult<Boolean> deleteCarRecord(@RequestParam("id") Long id) {
        carRecordService.deleteCarRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户-车子-资产记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hutai:car-record:query')")
    public CommonResult<CarRecordRespVO> getCarRecord(@RequestParam("id") Long id) {
        CarRecordDO carRecord = carRecordService.getCarRecord(id);
        return success(CarRecordConvert.INSTANCE.convert(carRecord));
    }

    @GetMapping("/list")
    @Operation(summary = "获得用户-车子-资产记录列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('hutai:car-record:query')")
    public CommonResult<List<CarRecordRespVO>> getCarRecordList(@RequestParam("ids") Collection<Long> ids) {
        List<CarRecordDO> list = carRecordService.getCarRecordList(ids);
        return success(CarRecordConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户-车子-资产记录分页")
    @PreAuthorize("@ss.hasPermission('hutai:car-record:query')")
    public CommonResult<PageResult<CarRecordRespVO>> getCarRecordPage(@Valid CarRecordPageReqVO pageVO) {
        PageResult<CarRecordDO> pageResult = carRecordService.getCarRecordPage(pageVO);
        return success(CarRecordConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户-车子-资产记录 Excel")
    @PreAuthorize("@ss.hasPermission('hutai:car-record:export')")
    @OperateLog(type = EXPORT)
    public void exportCarRecordExcel(@Valid CarRecordExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<CarRecordDO> list = carRecordService.getCarRecordList(exportReqVO);
        // 导出 Excel
        List<CarRecordExcelVO> datas = CarRecordConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "用户-车子-资产记录.xls", "数据", CarRecordExcelVO.class, datas);
    }

}
